package com.talent.service.iot.mqtt.codec.decoder;

import com.talent.service.iot.mqtt.model.fix.TlMqttFixedHead;
import com.talent.service.iot.mqtt.model.request.TlMqttPubRecReq;
import com.talent.service.iot.mqtt.model.variable.TlMqttPubRecVariableHead;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.mqtt.MqttMessageType;
import org.springframework.stereotype.Component;

/**
 * @ClassName：TlConnectDecoder
 * @Author: hszhou
 * @Date: 2024/11/30 15:12
 * @Description: Qos2的消息的消息回复
 */
@Component
public class TlMqttPubRecDecoder  {


    public TlMqttPubRecReq build(ByteBuf buf, int remainingLength){
        TlMqttFixedHead fixedHead = decodeFixedHeader(remainingLength);
        TlMqttPubRecVariableHead variableHead = decodeVariableHeader(buf);
        return new TlMqttPubRecReq(fixedHead,variableHead);

    }

    TlMqttFixedHead decodeFixedHeader(int remainingLength) {
        TlMqttFixedHead fixedHead =new TlMqttFixedHead();
        fixedHead.setMessageType(MqttMessageType.PUBREC);
        fixedHead.setLength(remainingLength);
        return fixedHead;
    }


    TlMqttPubRecVariableHead decodeVariableHeader(ByteBuf buf) {
        int messageId = buf.readUnsignedShort();
        return TlMqttPubRecVariableHead.of(messageId);
    }

}
